package com.lyncs.ods.modules.company.controller;


import com.lyncs.ods.common.api.CommonPage;
import com.lyncs.ods.common.api.CommonResult;
import com.lyncs.ods.modules.company.service.CompanyService;
import com.lyncs.ods.req.*;
import com.lyncs.ods.resp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 公司账号信息表 前端控制器
 * </p>
 *
 * @author alex
 * @since 2022-02-15
 */
@RestController
@RequestMapping("/company")
@Api(tags = {"企业相关接口"})
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    /**
     * 注册公司并关联到当前用户
     *
     * @param req company info
     * @return T/F
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户创建公司")
    public CommonResult<UserCompanyResp> registerCompany(@Valid @RequestBody SaveCompanyReq req) {
        return CommonResult.success(companyService.registerCompany(req));
    }

    /**
     * 修改企业信息
     *
     * @param req company info
     * @return T/F
     */
    @PostMapping("{companyId}/save")
    @ApiOperation(value = "修改企业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司ID", dataType = "Long", paramType = "path"),
    })
    public CommonResult<?> updateCompany(@PathVariable Long companyId, @Valid @RequestBody SaveCompanyReq req) {
        companyService.updateCompany(companyId, req);
        return CommonResult.success();
    }

    /**
     * create one company for partner
     *
     * @return T/F
     */
    @PostMapping("partner/save")
    @ApiOperation(value = "新增一个合作企业 用于发货/交付/结算等")
    public CommonResult<Long> savePartnerCompany(@RequestBody @Valid SavePartnerCompanyReq req) {
        return CommonResult.success(companyService.savePartnerCompany(req));
    }

    /**
     * list all partner company()
     *
     * @return partner companies
     */
    @GetMapping("partner/list")
    @ApiOperation(value = "通讯录：获取合作伙伴公司")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "请求类型：1.请求合作供应商公司;2.请求合作客户公司", dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "page", value = "当前页数，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page_size", value = "当前页请求数量，默认20", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "company_id", value = "企业ID", dataType = "Long")
    })
    public CommonResult<CommonPage<PartnerInfoResp>> listPartnerCompanies(@RequestParam(name = "type", defaultValue = "1") Integer type,
                                                                          @RequestParam(name = "page", defaultValue = "1") Integer page,
                                                                          @RequestParam(name = "page_size", defaultValue = "20") Integer pageSize,
                                                                          @RequestParam(name = "company_id", required = false) Long companyId) {
        return CommonResult.success(companyService.listPartnerCompanies(type, page, pageSize, companyId));
    }

    /**
     * release partner relation
     */
    @PostMapping("partner/relation")
    @ApiOperation(value = "建立关联关系")
    public CommonResult<?> relateContact(@Valid @RequestBody PartnerRelationReq req) {
        companyService.relateContact(req);
        return CommonResult.success();
    }

    /**
     * release partner relation
     */
    @GetMapping("partner/release")
    @ApiOperation(value = "取消关联关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "company_id", value = "企业ID", dataType = "Long"),
            @ApiImplicitParam(name = "type", value = "类型:1.解除关联;2.删除好友关系;默认1", dataType = "Integer", allowableValues = "1,2")
    })
    public CommonResult<?> releaseContact(@RequestParam("company_id") Long companyId, @RequestParam(value = "type", defaultValue = "1") Integer type) {
        companyService.releaseContact(companyId, type);
        return CommonResult.success();
    }

    /**
     * release partner relation
     */
    @GetMapping("partner/count")
    @ApiOperation(value = "关系统计，返回数据中，key为关系类型，value为数量")
    public CommonResult<Map<Integer, Long>> partnerCount() {
        return CommonResult.success(companyService.partnerCount());
    }

    /**
     * get company staffs by company id
     *
     * @return company staff info
     */
    @GetMapping("{companyId}/staffs")
    @ApiOperation(value = "获取公司员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司ID", dataType = "Long", paramType = "path"),
    })
    public CommonResult<List<StaffInfoResp>> listCompanyStaffs(@PathVariable Long companyId) {
        //获取公司员工信息
        return CommonResult.success(companyService.listCompanyStaffs(companyId));
    }

    @GetMapping("{companyId}/staffs/detail")
    @ApiOperation(value = "获取员工详情")
    public CommonResult<StaffDetailInfoResp> getStaffDetail(@PathVariable Long companyId, @RequestParam(value = "user_id") Long userId) {
        return CommonResult.success(companyService.getStaffDetail(companyId, userId));
    }

    /**
     * save company staffs by company/user id
     */
    @PostMapping("{companyId}/saveStaffBatch")
    @ApiOperation(value = "批量更新员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司ID", dataType = "Long", paramType = "path")
    })
    public CommonResult<?> saveStaffBatch(@PathVariable Long companyId, @Valid @RequestBody SaveStaffReq req) {
        companyService.saveStaffBatch(companyId, req);
        return CommonResult.success();
    }

    @PostMapping("{companyId}/staffs/saveGroup")
    @ApiOperation(value = "保存员工权限组信息")
    public CommonResult<?> saveStaffGroup(@PathVariable Long companyId, @Valid @RequestBody SaveStaffGroupReq req) {
        companyService.saveStaffGroup(companyId, req);
        return CommonResult.success();
    }

    @GetMapping("{companyId}/payments")
    @ApiOperation(value = "获取公司收款方式属性信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "company_id", value = "公司ID", dataType = "Long", paramType = "path"),
    })
    public CommonResult<List<PaymentSettingResp>> listPaymentInfo(@PathVariable Long companyId) {
        return CommonResult.success(companyService.listPaymentInfo(companyId));
    }

    /*@PostMapping("/payment/save")
    @ApiOperation(value = "保存公司配置收款信息")
    public CommonResult<?> savePaymentInfo(@RequestBody SavePaymentReq req) {
        companyService.savePaymentInfo(req);
        return CommonResult.success();
    }*/

    @PostMapping("/payment/save")
    @ApiOperation(value = "企业收款方式数据更新")
    public CommonResult<?> updatePayment(@Valid @RequestBody CompanyPaymentEditReq req) {
        companyService.savePaymentInfo(req);
        return CommonResult.success();
    }

    @GetMapping("{companyId}/fees")
    @ApiOperation(value = "获取结算款项配置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "company_id", value = "公司ID", dataType = "Long", paramType = "path"),
    })
    public CommonResult<List<String>> listCompanyFees(@PathVariable Long companyId) {
        //获取公司员工信息
        return CommonResult.success(companyService.listCompanyFees(companyId));
    }

    @PostMapping("/fee/save")
    @ApiOperation(value = "保存结算款项配置信息，传所有款项")
    public CommonResult<?> saveCompanyFees(@RequestParam("fee_names") List<String> feeNames) {
        companyService.saveCompanyFees(feeNames);
        return CommonResult.success();
    }

    /**
     * get one company detail info by id
     *
     * @return detail info
     */
    @GetMapping("{companyId}/detail")
    @ApiOperation(value = "获取公司详细信息。companyId：获取公司详情；partnerId：获取合作企业详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "company_id", value = "公司ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "类型，默认company。company:A类企业;virtual:B类企业;partner:合作企业.", dataType = "String", paramType = "query", allowableValues = "company,virtual,partner"),
    })
    public CommonResult<CompanyDetailInfoResp> getCompanyDetail(@PathVariable Long companyId, @RequestParam(value = "type", defaultValue = "company") String type) {
        return CommonResult.success(companyService.getCompanyDetailInfo(companyId, type));
    }

    @GetMapping("{companyId}/spu/list")
    @ApiOperation(value = "获取公司关联spu信息。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "company_id", value = "公司ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "page", value = "当前页数，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page_size", value = "当前页请求数量，默认20", dataType = "Integer", paramType = "query"),
    })
    public CommonResult<CommonPage<SpuInfoResp>> listCompanySpu(@PathVariable String companyId,
                                                                @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                @RequestParam(value = "page_size", required = false, defaultValue = "20") Integer pageSize) {
        return CommonResult.success(companyService.listCompanySpu(companyId, page, pageSize));
    }

    @GetMapping("{companyId}/spu/relation")
    @ApiOperation(value = "公司关联spu")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "company_id", value = "公司ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "spu_id", value = "spu ID", dataType = "Long"),
            @ApiImplicitParam(name = "type", value = "操作类型：1.关联;2.解绑", dataType = "Integer"),
    })
    public CommonResult<?> saveCompanySpu(@PathVariable Long companyId, @RequestParam("spu_id") Long spuId, Integer type) {
        companyService.saveCompanySpu(companyId, spuId, type);
        return CommonResult.success();
    }

    @GetMapping("{companyId}/staffs/delete")
    @ApiOperation(value = "删除公司员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "user_id", value = "用户ID", dataType = "Long", paramType = "query"),
    })
    public CommonResult<?> deleteStaff(@PathVariable Long companyId, @RequestParam("user_id") Long userId) {
        companyService.deleteStaff(companyId, userId);
        return CommonResult.success();
    }

    @GetMapping("{companyId}/staffs/role")
    @ApiOperation(value = "更新公司员工权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "user_id", value = "用户ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "role", value = "权限ID: 1.admin", dataType = "Integer", paramType = "query"),
    })
    public CommonResult<?> updateRole(@PathVariable Long companyId, @RequestParam("user_id") Long userId, @RequestParam(value = "role", defaultValue = "1") Integer role) {
        companyService.updateRole(companyId, userId, role);
        return CommonResult.success();
    }

    @GetMapping("partner/type")
    @ApiOperation(value = "获取合作企业关联关系，此接口只能用于查看分享接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "company_id", value = "邀请方企业ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "relation_id", value = "受邀企业ID", dataType = "Long", paramType = "query"),
    })
    public CommonResult<String> getPartnerType(@RequestParam("company_id") Long companyId, @RequestParam("relation_id") Long relationId) {
        return CommonResult.success(companyService.getPartnerType(companyId, relationId));
    }

    @PostMapping("role/save")
    @ApiOperation(value = "保存权限组（标签）")
    public CommonResult<?> saveRoleGroup(@Valid @RequestBody SaveCompanyRoleGroupReq req) {
        companyService.saveRoleGroup(req);
        return CommonResult.success();
    }

    @GetMapping("role/list")
    @ApiOperation(value = "获取当前登录企业的权限组列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数，非必填，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page_size", value = "每页数量，非必填，默认50", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "include_company_info", value = "是否包含企业信息，非必填，默认false", dataType = "Boolean", paramType = "query"),
    })
    public CommonResult<CommonPage<CompanyRoleGroupResp>> listRoleGroup(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                        @RequestParam(value = "page_size", required = false, defaultValue = "50") Integer pageSize,
                                                                        @RequestParam(value = "include_company_info", required = false, defaultValue = "false") Boolean includeCompanyInfo) {
        return CommonResult.success(companyService.listRoleGroup(page, pageSize, includeCompanyInfo));
    }

    @GetMapping("seal/update")
    @ApiOperation(value = "更新企业印章URL")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "seal", value = "印章URL", dataType = "String", paramType = "query")
    })
    public CommonResult<?> updateSeal(@RequestParam("seal") String seal) {
        companyService.updateSeal(seal);
        return CommonResult.success();
    }

    @GetMapping("seal/get")
    @ApiOperation(value = "获取企业印章URL")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "company_id", value = "企业ID", dataType = "Long", paramType = "query")
    })
    public CommonResult<String> getSeal(@RequestParam("company_id") Long companyId) {
        return CommonResult.success(companyService.getSeal(companyId));
    }

    @GetMapping("transInfo")
    @ApiOperation(value = "获取企业交易信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "company_id", value = "企业ID", dataType = "Long", paramType = "query")
    })
    public CommonResult<TransactionInfoResp> getTransactionInfo(@RequestParam("company_id") Long companyId) {
        return CommonResult.success(companyService.getTransactionInfo(companyId));
    }
}

